mod sqlite_tool;

use crate::sqlite_tool::{check_move_table_col, DBInfo};
use rusqlite::{Connection, Result};
use sqlite_tool::check_add_table_col;
use std::borrow::Borrow;
use std::env;
use std::ops::DerefMut;
use std::process::exit;

#[derive(Debug)]
struct Person {
    id: i32,
    name: String,
    data: Option<Vec<u8>>,
}

fn main() -> Result<()> {
    let args: Vec<String> = env::args().collect();
    if args.len() < 1 {
        println!(
            "\
usage: ./sqlite_update -update sqlite_old.db sqlite_new.db
       ./sqlite_update -move sqlite_old.db column1 column2
        "
        );
        exit(1);
    }

    match args[1].as_str() {
        "update" => {
            if args.len() < 3 {
                println!(
                    "\
                usage: ./sqlite_update -update sqlite_old.db sqlite_new.db
                "
                );
                exit(2);
            }
            let mut old_db =
                Box::new(DBInfo::read_db(args[2].as_str())).expect("read old db failed!");
            let new_db = Box::new(DBInfo::read_db(args[3].as_str())).expect("read new db failed!");
            check_add_table_col(&mut old_db, &new_db)
                .expect("check and add table column failed!");
        }
        "move" => {
            if args.len() < 4 {
                println!(
                    "\
                usage: ./sqlite_update -move sqlite_old.db table_name from_column to_column
                "
                );
                exit(2);
            }
            let mut db = Box::new(DBInfo::read_db(args[2].as_str())).expect("read db failed!");
            check_move_table_col(db, args[3].as_str(), args[4].as_str(), "")
                .expect("check move table column failed!");
        }
        _ => {
            println!(
                "\
usage: ./sqlite_update -update sqlite_old.db sqlite_new.db
       ./sqlite_update -move sqlite_old.db column1 column2
        "
            );
        }
    }

    Ok(())
}
